var request = require('request')
var $conf = require('./conf') // 配置地址文件
const redis = $conf.redis
const schedule = require('node-schedule') // 定时任务
module.exports = {
  scheduleJob: true,
  createMenu: function (param) {
    var _this = this
    var opts = {

      url: `https://api.weixin.qq.com/cgi-bin/menu/create?access_token=${param.access_token}`,
      method: 'post',
      body: JSON.stringify({
        'button': [{
          'name': '泰国指南',
          'type': 'view',
          'url': 'https://thai.zanshang520.com'
        }, {
                    // "type": "click",
                    // "name": "城市",
                    // "sub_button": [{
                    //     "type": "view",
                    //     "name": "曼谷",
                    //     "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5dce61ae8831d09f&redirect_uri=https%3a%2f%2fthai.zanshang520.com%2fthai&response_type=code&scope=snsapi_base&state=bangkok#wechat_redirect"
                    // }, {
                    //     "type": "view",
                    //     "name": "芭提雅",
                    //     "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5dce61ae8831d09f&redirect_uri=https%3a%2f%2fthai.zanshang520.com%2fthai&response_type=code&scope=snsapi_base&state=pattaya#wechat_redirect"
                    // }, {
                    //     "type": "view",
                    //     "name": "清迈",
                    //     "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5dce61ae8831d09f&redirect_uri=https%3a%2f%2fthai.zanshang520.com%2fthai&response_type=code&scope=snsapi_base&state=chiangmai#wechat_redirect"
                    // }, {
                    //     "type": "view",
                    //     "name": "普吉",
                    //     "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5dce61ae8831d09f&redirect_uri=https%3a%2f%2fthai.zanshang520.com%2fthai&response_type=code&scope=snsapi_base&state=phuket#wechat_redirect"
                    // }, {
          'type': 'view',
          'name': '苏梅岛',
          'url': 'https://thai.zanshang520.com/thai'
                        // }]
        }]
      })
    }

    request(opts, function (err, response, body) {
      console.log(body)
    })
  },
    // 获取accesstoken
  getAccesstoken: function (param, callback) {
    var _this = this
    var url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${$conf.appid}&secret=${$conf.appsecret}`
    request(url, function (err, response, body) {
      try {
        var body = JSON.parse(body)
        console.log(body)
        callback(err, body)

        redis.set('access_token', body.access_token) // 设置

        if (_this.scheduleJob) {
          _this.createMenu(body)
                    // 每小时的30分钟更新一次吃token
          _this.updatetAccesstoken(30)
          _this.scheduleJob = false
        }
      } catch (error) {
        console.error(error)
                    // callback(error, null);
      }
    })
  },
    // 定时任务,每小时运行一次(需要查看定时任务是否执行)
  updatetAccesstoken: function (time) {
    var _this = this
    var rule = new schedule.RecurrenceRule()
    rule.minute = time
        // rule.second = time;

    var j = schedule.scheduleJob(rule, function () {
      _this.getAccesstoken(param, function (err, ret) {
        console.log('scheduleJob doing and update access_token')
      })
    })
  }
}
